<?php

namespace app\home\controller;

use think\Db;

class Order extends Base
{
    protected $beforeActionList = [
        'first',
    ];

    protected function first()
    {
        if (session('username')) {
            $this->assign('nav', 'user');
        } else {
            $this->redirect('/home/user/index');
        }
    }


    /*已购产品*/
    public function runorder()
    {
        return view();
    }

    /*购买产品*/
    public function buyorder()
    {
        $post = $this->request->param();
        if ($this->request->isPost()) {
            $validate = new \app\admin\validate\News();
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            Db::startTrans();
            try {
                $post['c_time'] = time();
                $post['info'] = $this->request->param('info', '', null);

                if (isset($post['id']) && !empty($post['id'])) {
                    preg_match_all('/(?=\/qiniu).+?(?=")/', $post['info'], $files_new);
                    $info = Db::name('news')->where(['id' => $post['id']])->find();
                    $qiniu = new Qiniu();
                    preg_match_all('/(?=\/qiniu).+?(?=")/', $info['info'], $files);
                    foreach ($files[0] as $item) {
                        if (in_array($item, $files_new[0])) {

                        } else {
                            $qiniu->delqiniu($item);
                        }
                    }
                    DB::name('news')->update($post);
                } else {
                    DB::name('news')->insert($post);
                }
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        } else {
            if (isset($post['id']) && !empty($post['id'])) {
                $info = DB::name('product_info')->where('id', $post['id'])->find();
                $info['product_title'] = DB::name('product')->where('status', '<', 8)->where('id', $info['product_id'])->value('title');
                $this->assign('info', $info);
                $buyorder = DB::name('product_run')->where('uid', session('uid'))->select();
                $this->assign('buyorder', $buyorder);
            } else {
                $this->error('参数错误，请稍后再试');
            }
            return view();
        }
    }

    /*产品列表*/
    public function runorderlists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        $map[] = ['product_run.uid', '=', session('uid')];
        $data['count'] = Db::view('product', ['id' => 'product_id', 'title'])
            ->view('product_run', 'id,product_id,s_time,e_time,uid,domain,status,info', 'product.id=product_run.product_id')
            ->where($map)
            ->count();
        $data['data'] = Db::view('product', ['id' => 'product_id', 'title'])
            ->view('product_run', 'id,product_id,s_time,e_time,uid,domain,diy_domain,status,info', 'product.id=product_run.product_id')
            ->where($map)
            // ->fetchSql(true)
            ->order(['product.status' => 'desc', 'product_run.id' => 'desc'])
            ->page(input("param.page"), input('param.limit'))->select();
        foreach ($data['data'] as &$info) {
            $info['e_time'] = date('Y-m-d', $info['e_time']);
            $info['domain'] = $info['diy_domain'] ? $info['diy_domain'] : $info['diy_domain'];
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    /*订单历史*/
    public function outorder()
    {
        return view();
    }

    /*发票*/
    public function tax()
    {
        return view();
    }

    /*发票列表*/
    public function taxlists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();

        $data['count'] = Db::name('tax')
            ->where('uid', session('uid'))
            ->count();
        $data['data'] = Db::name('tax')
            ->where('uid', session('uid'))
            ->where($map)
            // ->fetchSql(true)
            ->order(['id' => 'desc'])
            ->page(input("param.page"), input('param.limit'))->select();
        $status = config('param.tax_status');
        foreach ($data['data'] as &$item) {
            $item['status'] = $status[$item['status']];
            $item['c_time'] = date('Y-m-d H:i:s', $item['c_time']);
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    /*申请发票*/
    public function addtax()
    {
        $post = $this->request->param();
        //服务包定单总金额,
        $sum = DB::name('product_order')
            ->where('status', 3)//状态为支付完成
            ->where('uid', session('uid'))->sum('price');
        //已经开具金额
        $tax_out = DB::name('tax')
            ->where('status', 'in', '1,3，4')//状态为申请开票或开票完成开票寄出
            ->where('uid', session('uid'))->sum('price');
        //申请时6个月以来的金额
        $d_180 = DB::name('product_order')
            ->where('status', 3)//状态为支付完成
            ->where('c_time', '>', time() - 86400 * 30 * 6)//180天
            ->where('uid', session('uid'))->sum('price');
        //计算可开票
        $tax_price = $d_180 > ($sum - $tax_out) ? ($sum - $tax_out) : $d_180;
        if ($this->request->isPost()) {
            if ($post['price'] > $tax_price) {
                $this->error('开票金额超过可开票金额');
            }
            $info = DB::name('user')->where('id', session('uid'))->find();
            $post['company'] = $info['company'];
            $post['inname'] = $info['inname'];
            $post['inmobile'] = $info['inmobile'];
            $post['address'] = $info['address'];
            $post['uid'] = session('uid');
            $post['c_time'] = time();

            $validate = new \app\home\validate\Tax();
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            // dump($post);
            Db::startTrans();
            try {
                DB::name('tax')->strict(false)->insert($post);

                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        } else {
            $info['sum'] = $sum;
            $info['tax_out'] = $tax_out;
            $info['d_180'] = $d_180;
            $info['price'] = $tax_price;
            $this->assign('info', $info);
            return view();
        }
    }

    public function taxinfo()
    {

        if ($this->request->isPost()) {

            $this->success('操作成功!');

        } else {
            $info = DB::name('tax')->where('id', input('param.id'))->find();
            $status = config('param.tax_status');
            $info['status']=$status[$info['status']];
            $info['c_time']=date('Y-m-d H:i:s', $info['c_time']);
            $this->assign('info', $info);
            return view();
        }
    }

    /*产品列表*/
    public function outorderlists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        if (isset($post['product_run_id']) && !empty($post['product_run_id'])) {
            $map[] = ['product_run_id', '=', $post['product_run_id']];
        }
        $map[] = ['product_order.uid', '=', session('uid')];
        $data['count'] = Db::view('product', ['id' => 'product_id', 'title'])
            ->view('product_order', 'id,product_id,c_time,uid,price,status,day,order_id', 'product.id=product_order.product_id')
            ->view('product_info', ['title' => 'info_title'], 'product_order.product_info_id=product_info.id')
            ->where($map)
            ->count();
        $data['data'] = Db::view('product', ['id' => 'product_id', 'title'])
            ->view('product_order', 'id,product_id,c_time,uid,price,status,day,order_id', 'product.id=product_order.product_id')
            ->view('product_info', ['title' => 'info_title'], 'product_order.product_info_id=product_info.id')
            ->where($map)
            ->order(['product_order.id' => 'desc'])
            ->page(input("param.page"), input('param.limit'))->select();
        $status = config('param.order_status');
        foreach ($data['data'] as &$item) {
            $item['status'] = $status[$item['status']];
            $item['c_time'] = date('Y-m-d H:i:s', $item['c_time']);
            $item['title'] = $item['title'] . '|' . $item['info_title'];
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    /*产品订单*/
    public function order()
    {
        return view();
    }

    /*产品列表*/
    public function orderlists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        $map[] = ['product_info.status', '=', 1];
        $map[] = ['product_info.status', '<', 3];
        $data['count'] = Db::view('product_info', 'id,title,price,day,status,product_id')
            ->view('product', ['id' => 'product_id', 'title' => 'product_title'], 'product_info.product_id=product.id')
            ->where($map)
            ->count();
        $data['data'] = Db::view('product_info', 'id,title,price,day,status,product_id')
            ->view('product', ['id' => 'product_id', 'title' => 'product_title'], 'product_info.product_id=product.id')
            ->where($map)
            // ->fetchSql(true)
            ->order('id desc')->page(input("param.page"), input('param.limit'))->select();
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }
}